<?php
class MSEP
{


      private static $objects = array();



      private static $settings = array();



      private static $name = 'MSEP Core';



      private static $version = '3.02';



      private static $instance;



      private function __construct()
      {
            global $user, $owner, $smarty, $admin, $page, $msep_messages;

            $this->stores($user, 'user');

            $this->stores($owner, 'owner');

            $this->stores($smarty, 'smarty');

            $this->stores($admin, 'admin');

            $this->stores($page, 'page');

            $included_files = get_included_files();

            if (preg_match("/admin([a-zA-Z0-9\|\-_]+)\.php$/", $included_files[0]))
            {
                  $this->stores('.', 'dot');
            }
            else
            {
                  $this->stores('', 'dot');
            }

            if ($handle = opendir($this->__('dot').'./include/msep/data/messages/'))
            {
                  while (false !== ($file = readdir($handle)))
                  {
                        if ($file != "." && $file != ".." && $file != "index.php")
                        {
                              include $this->__('dot').'./include/msep/data/messages/'.$file;
                        }
                  }

                  closedir($handle);
            }

            if ($handle = opendir($this->__('dot').'./include/msep/data/pages/'))
            {
                  while (false !== ($file = readdir($handle)))
                  {
                        if ($file != "." && $file != ".." && $file != "index.php")
                        {
                              include $this->__('dot').'./include/msep/data/pages/'.$file;

                              if (in_array($this->__('page'), $msep_pages))
                              {
                                    $this->stores(substr($file, 0, -4), 'plugin');
                              }
                        }
                  }

                  closedir($handle);
            }

            $this->storem($msep_messages);

            $this->stores("./include/msep/includes/js/core.js?v=".self::$version, "plugin_js", true);

            $this->__('smarty')->assign("msep_js", $this->__('plugin_js'));
      }



      public static function init()
      {
            if(!isset(self::$instance))
            {
                  $obj = __CLASS__;
                  self::$instance = new $obj;
            }

            return self::$instance;
      }



      public function __clone()
      {
            trigger_error( 'Cloning the registry is not permitted', E_USER_ERROR );
      }



      public function setPlugin($plugin, $use_page = true)
      {
            global $page;

            $plugin = preg_replace("/[^a-z]/", "", $plugin);
            $plugin_exists = $this->_('database')->query("SELECT plugin_id FROM se_plugins WHERE plugin_type='{$plugin}'", true)->boolean();

            if (!$use_page)
            {
                  $this->stores($plugin, 'plugin');
            }
            elseif ($use_page && preg_match("/({$plugin}_(.*?)|(.*?)_{$plugin}|{$plugin}s_(.*?)|(.*?)_{$plugin}s|{$plugin}|{$plugin}s)/", $page) && $plugin_exists)
            {
                  $this->stores($plugin, 'plugin');
            }

            if ($plugin_exists)
            {
                  $this->stores($plugin, 'plugins', true);
            }
      }



      private function store($object, $key)
      {
            $class_name = strtolower(str_replace("MSEP_", "", $object));

            if (file_exists($this->__('dot')."./include/msep/objects/{$class_name}.class.php"))
            {
                  require_once($this->__('dot')."./include/msep/objects/{$class_name}.class.php");
            }
            elseif (file_exists($this->__('dot')."./include/msep/plugins/{$class_name}.class.php"))
            {
                  require_once($this->__('dot')."./include/msep/plugins/{$class_name}.class.php");

                  $plugin_js = $this->__('dot')."./include/msep/includes/js/{$class_name}.js";

                  if (file_exists($plugin_js))
                  {
                        $plugin_info = $this->_('database')->fetch_assoc($this->_('database')->query("SELECT plugin_version FROM se_plugins WHERE plugin_type = '{$class_name}' LIMIT 1"));

                        $this->stores($this->__('dot')."./include/msep/includes/js/{$class_name}.js?v={$plugin_info['plugin_version']}", "plugin_js", true);
                  }

                  $this->__('smarty')->assign("msep_js", $this->__('plugin_js'));

                  if ($this->__('user')->user_exists)
                  {
                        $this->__('user')->user_info['paths'][$class_name] = $this->_('misc')->user_dir($this->__('user')->user_info['user_id'], $class_name);
                  }

                  if ($this->__('owner')->user_exists)
                  {
                        $this->__('owner')->user_info['paths'][$class_name] = $this->_('misc')->user_dir($this->__('owner')->user_info['user_id'], $class_name);
                  }
            }

            $obj = new $object();
            $obj->__init(self::$instance);

            self::$objects[$key] = $obj;
      }



      public function _($key)
      {
            if(is_object(self::$objects[$key]))
            {
                  return self::$objects[$key];
            }
            else
            {
                  $object = 'MSEP_'.strtoupper(substr($key, 0, 1)).substr($key, 1);
                  $this->store($object, $key);

                  return self::$objects[$key];
            }
      }



      public function get($key)
      {
            return $this->_($key);
      }



      public function stores($data, $key, $is_array = false, $sub_key = "")
      {
            if ($is_array && $sub_key != "")
            {
                  self::$settings[$key][$sub_key] = $data;
            }
            elseif ($is_array)
            {
                  self::$settings[$key][] = $data;
            }
            else
            {
                  self::$settings[$key] = $data;
            }
      }



      public function &gets($key)
      {
            return self::__($key);
      }



      public function &__($key)
      {
            return self::$settings[$key];
      }



      public function removes($key)
      {
            unset(self::$settings[$key]);
      }



      public function storem($messages)
      {
            foreach ($messages as $array)
            {
                  foreach ($array as $index => $message)
                  {
                        $this->stores($message, 'messages', true, $index);
                  }
            }
      }



      public function version()
      {
            return self::$version;
      }
}



abstract class MSEP_Plugin
{


      public $core;



      abstract protected function __init(MSEP $core);


}



abstract class MSEP_Object
{


      public $core;



      abstract protected function __init(MSEP $core);


}
?>